package com.job.service.impl;

import com.job.common.Check;
import com.job.common.Constant;
import com.job.common.ResponseResult;
import com.job.common.asserts.MyAssert;
import com.job.common.enums.ResultCodeEnum;
import com.job.common.exception.CityIsNullException;
import com.job.common.exception.ProvinceIsNullException;
import com.job.dao.AreaDao;
import com.job.pojo.City;
import com.job.pojo.Province;
import com.job.service.AreaService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AreaServiceImpl implements AreaService, Constant {
    @Resource
    private AreaDao areaDao;

    @Override
    public ResponseResult<List<Province>> provinceQuery() throws ProvinceIsNullException {
        List<Province> provinces = areaDao.queryAllProvince();
        MyAssert.ProvinceCheck(provinces);
        return new ResponseResult<List<Province>>().success(provinces);
    }

    @Override
    public ResponseResult<List<City>> cityQuery(String provinceId) throws CityIsNullException {
        int id = Check.checkAndReturnId(provinceId);
        if (id == INVALID_SCAN){
            return new ResponseResult<List<City>>().fail(ResultCodeEnum.CITY_IS_NULL_FAIL);
        }

        List<City> cities = areaDao.queryCityByProvinceId(id);
        MyAssert.CityCheck(cities);
        return new ResponseResult<List<City>>().success(cities);
    }
}
